using Microsoft.AspNetCore.Mvc;
using Admin2000.Backend.Api.Entity;
using System.Linq;
using Admin2000.Backend.Api.Params;
using Admin2000.Backend.Api.Repository;


namespace Admin2000.Backend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        private IRepository<Users> _usersRespoitory = new EfRepository<Users>();


        //get请求 查询所有的用户信息
        public dynamic Get()
        {
            var users = _usersRespoitory.Table.ToList();
            return new
            {
                Code = 1000,
                Data = users,
                Msg = "获取用户列表成功^_^"
            };
        }



        //get请求 查询user表中参数为id的信息
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var user = _usersRespoitory.GetById(id);
            return new
            {
                Code = 1000,
                Data = user,
                Msg = "获取指定用户成功"
            };
        }

        //post请求 用户用户新增    
        [HttpPost]
        public dynamic Post(CreateUser newUser)
        {

            var username = newUser.Username.Trim();
            var password = newUser.Password.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "创建用户失败 用户名或者密码不能为空"
                };
            }


            var user = new Users
            {
                Username = newUser.Username,
                Password = newUser.Password
            };
            _usersRespoitory.Insert(user);

            return new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功"
            };

        }


        //put请求 用于用户修改编辑信息
        [HttpPut("{id}")]
        public dynamic Put(int id, CreateUser updateUser)
        {
            var username = updateUser.Username.Trim();
            var password = updateUser.Password.Trim();

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "用户名或者密码不能为空"
                };
            }

            var user = _usersRespoitory.GetById(id);
            if (user == null)
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "要更新的用户不存在，请确认后重试=_="
                };
            }

            user.Username = updateUser.Username;
            user.Password = updateUser.Password;

            _usersRespoitory.Update(user);

            return new {
                Code =1000,
                Data =user,
                Msg = "修改用户信息成功"
            };

        }


        [HttpDelete("{id}")]

        public dynamic Delete( int id )
        {

            _usersRespoitory.Delete(id);
            return new {
                Code =1000,
                Data = "",
                Msg ="删除用户成功"
            };

        }

    }

}